﻿using Basic.Api.Filters;
using Basic.Api.Models;
using OF.Component.BLL;
using OF.Component.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using OF.Utility;

namespace Basic.Api.Controllers
{
    /// <summary>
    /// 用户统一身份证认证接口
    /// </summary>
    public class PassportController : ApiController
    {

        /// <summary>
        /// 用户认证接口
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        public ResultData<ResPassportUserInfo> Authenticate(ReqPassportAuthenticate  reqInfo)
        {
            var result=new ResultData<ResPassportUserInfo>();
            var oInfo = AuthorityUserInforOperate.QuerySingle(m => m.LoginName == reqInfo.UserName);
            if (oInfo != null)
            {
                var encryptPwd = SecurityUtility.EncryptAsc(reqInfo.Password);
                if (encryptPwd == oInfo.PassWord)
                {
                    var oRes=new ResPassportUserInfo();
                    oInfo.MergeTo(oRes);
                    result.data = oRes;
                    result.status = 0;
                }
                else
                {
                    result.status = -1;
                    result.message = "密码不正确";
                }
            }
            else
            {
                result.status = -1;
                result.message = "用户名不存在";
            }
            return result;
        }


        /// <summary>
        /// 根据用户ID获取用户信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public ResultData<ResPassportUserInfo> GetUserInfoByID(int userID)
        {
            var result = new ResultData<ResPassportUserInfo>();
            var oInfo = AuthorityUserInforOperate.GetByID(userID);
            if (oInfo != null)
            {
                var oRes = new ResPassportUserInfo();
                oInfo.MergeTo(oRes);

                result.data = oRes;
                result.status = 0;
            }
            else
            {
                result.status = -1;
                result.message = "用户信息不存在";
            }
            return result;
        }

        /// <summary>
        /// 添加登录认证信息
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        public ResultData AddPassportSignIn(ReqPassportSignIn reqInfo)
        {
            var result=new ResultData();
            var oExist = PassportSignInOperate.QuerySingle(m => m.SignInID == reqInfo.SignInID);
            if (oExist != null)
            {
                var oInfo = new PassportSignInInfo();
                oExist.MergeTo(oInfo);
                PassportSignInOperate.Update(oInfo);
            }
            else
            {
                var oInfo = new PassportSignInInfo();
                reqInfo.MergeTo(oInfo);
                PassportSignInOperate.Insert(oInfo);
            }
            result.status = 0;
            result.message = "";
            return result;
        }

        /// <summary>
        /// 添加应用认证票信息
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        public ResultData AddPassportTicket(ReqPassportTicket reqInfo)
        {
            var result = new ResultData();
            var oExist = PassportTicketOperate.QuerySingle(m => m.AppSignInID == reqInfo.AppSignInID);
            if (oExist != null)
            {
                var oInfo = new PassportTicketInfo();
                oExist.MergeTo(oInfo);
                PassportTicketOperate.Update(oInfo);
            }
            else
            {
                var oInfo = new PassportTicketInfo();
                reqInfo.MergeTo(oInfo);
                PassportTicketOperate.Insert(oInfo);
            }
         
            result.status = 0;
            result.message = "";
            return result;
        }

        /// <summary>
        /// 获取一个会话登录的所有App的登出地址
        /// </summary>
        /// <param name="sessionID"></param>
        /// <returns></returns>
        public ResultData<List<string>> GetAllAppLogOffUrl(string sessionID)
        {
            var result=new ResultData<List<string>>() ;
            var listReturn = new List<string>();
            var list = PassportTicketOperate.QueryList(m => m.SignInID == sessionID);
            if (list.Count > 0)
            {
                listReturn = list.Select(m => m.AppLogOffUrl).Distinct().ToList();
                result.data = listReturn;
                result.status = 0;
            }
            return result;
        }








    }
}